<?php
namespace gnomephp\message;

/**
 * 
 * A generic messaging class, used to show simple message widgets to users.
 * 
 * @author peec
 *
 */
class Message{
	/**
	 * 
	 * This is a information message.
	 * @var int
	 */
	const INFO = 1;
	/**
	 * 
	 * This is a Warning message.
	 * @var int
	 */
	const WARNING = 2;
	/**
	 * 
	 * This is a error message.
	 * @var int
	 */
	const ERROR = 3;
	/**
	 * 
	 * This is a success message.
	 * @var int
	 */
	const SUCCESS = 4;
	
	
	/**
	 * 
	 * Holds all the messages. Array of MessageItem's.
	 * @var array
	 */
	static protected $messages=array();
	
	
	/**
	 * 
	 * Adds a new message to the message stack.
	 * Messages should be looped in the view.
	 * @param int $type The type of message, can be: Message:INFO, Message:WARNING, Message:ERROR, Message:SUCCESS
	 * @param string $message The message to show.
	 * @throws MessageException
	 */
	static public function add($type, $message){
		switch($type){
			case self::ERROR:
				self::$messages[] = new ErrorMessage($type, $message);
				break;
			case self::SUCCESS:
				self::$messages[] = new SuccessMessage($type, $message);
				break;
			case self::INFO:
				self::$messages[] = new InfoMessage($type, $message);
				break;
			case self::WARNING:
				self::$messages[] = new WarningMessage($type, $message);
				break;
			default:
				throw new MessageException("No such message object exist. Availble message objects is: Message::ERROR, Message::SUCCESS, Message::INFO and Message::WARNING.");
		}
	}
	
	
	
	
	static public function deleteMessages(){
		self::$messages = array();
	}
	
	static public function getMessages(){
		$session = new \gnomephp\Session();
		$msgs = $session->get('gnomephp.messages');
		if ($msgs != null){
			
			
			return array_merge($msgs, self::$messages);
		}
		
		return self::$messages;
	}
	
	
	
	
}

